class Solution {
public:
    int shipWithinDays(vector<int>& weights, int days) {
        
int left=*max_element(weights.begin(),weights.end()),
right=accumulate(weights.begin(),weights.end(),0);
 while(left<right){
    int mid=(left+right)/2;
    int need=1,cur=0;
    for(int weight:weights){
        if(cur+weight>mid){
            ++need;
            cur=0;
        }
        cur+=weight;
    }
    if(need<=days){
        right=mid;
    }
    else{
        left=mid+1;
    }
 }
 return left;
    }
};
